package KK.net.Proxy
{
	import KK.Data.GameData;
	import KK.Data.ProtocolData;
	import KK.Event.GameSystemEvent;
	import KK.Event.ModuleEvent;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.Global.Game;
	import KK.net.ProxyManager;
	
	import Protocol.EProtocol;
	import Protocol.SBattleResult;
	import Protocol.SBuildingUpgrade;
	import Protocol.SLogin;
	import Protocol.SRegister;
	import Protocol.SUserInfo;
	
	import flash.utils.ByteArray;
	
	import mx.modules.Module;

	public class UserProxy extends Proxy
	{
		public function UserProxy()
		{
			//TODO: implement function
			super();
			init();
		}
		
		/**
		 * 初始化 
		 * 
		 */
		private function init():void{
			
			ProxyManager.RegisterCommand(EProtocol.EProtocol_UserInfo,onGetUserInfo);
			ProxyManager.RegisterCommand(EProtocol.EProtocol_BattleResult,onBattleResult);
		}
		
		
		
		/**
		 * 绑定时间  
		 * @param _event
		 * 
		 */
		override public function BindEvent(_event:IMessageQueue):Boolean{
			 super.BindEvent(_event);
			 initEvent();
			return true;
		}
		
		/**
		 *  
		 * 
		 */
		private function initEvent():void{
			AttentionMessage(GameSystemEvent.UPDATE_PUBLIC,onTest);
		}
		
		/**
		 * 发送测试  
		 * @param event
		 * 
		 */
		private function onTest(event:GameSystemEvent):void{
			/*
			var data:SRegister=new SRegister();
			data.userName=Game.OpenID;
			data.name='fawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewa';
			Send(EProtocol.EProtocol_Register,data);
			
			*/
			/* //登陆
			var data:SLogin=new SLogin();
			data.userName='fawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewafawefwaeewa';
//			Send(EProtocol.EProtocol_Register,data);
			var byte:ByteArray=ProtocolData.Creator(); 
			data.Serialize(byte);
			var i:int=0;
			var len:int=10000000;
			while(i<len){
				SendByte(EProtocol.EProtocol_Register,byte);
				i++;
			}
			*/
			/*
			var byte:ByteArray=ProtocolData.Creator();
			
			SendByte(EProtocol.EProtocol_BattleStart,byte);*/
			var data:SBuildingUpgrade=new SBuildingUpgrade();
			data.type=0;
			Send(EProtocol.EProtocol_BuildingUpgrade,data);
			
		}
		
		
		/**
		 * 战斗返回  
		 * @param data
		 * 
		 */
		private function onBattleResult(byte:ByteArray):void{
			var data:SBattleResult=new SBattleResult();
			
			data.Unserialize(byte);
			
			GameData.BattleData=data;
			Publish(new GameSystemEvent(GameSystemEvent.START_BATTLE));
			LogInfo("战斗返回 ");
		}
		
		/**
		 * 获取用户数据  
		 * @param data
		 * 
		 */
		private function onGetUserInfo(data:ByteArray):void{
			var user:SUserInfo=new SUserInfo();
			user.Unserialize(data);
			GameData.User=user.info;
			GameData.ServerTime=user.loginTime;
			Publish(new ModuleEvent(ModuleEvent.HIDE,GameData.ModuleID['VRegister']));
			Publish(new ModuleEvent(ModuleEvent.SHOW,GameData.ModuleID['SceneWord']));
			LogInfo(GameData.ServerTime); 
			Publish(new MSGEvent(GameSystemEvent.START_GAME));
		}
	}
}